package com.fx.controller;

import com.alibaba.fastjson.JSONObject;
import com.fx.entity.SysRoleRes;
import com.fx.service.SysRoleResService;
import com.fx.util.Constant;
import com.fx.util.PageInfo;
import com.fx.util.Query;
import com.fx.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @Auther: EDZ
 * @Date: 2018-5-25 16:40:
 * @Email: 你的邮箱
 * 概要说明：该类干嘛用的
 **/
@Controller
@RequestMapping("/sysRoleRes")
public class SysRoleResController extends BaseController {

    @Autowired
    SysRoleResService sysRoleResService;
    private static String pageTitle = "权限管理";
    private static String pageUrl = "/sysRoleRes";


    @RequestMapping()
    public String intoView(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.LIST_TITLE));
        return "sysRoleRes/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(@RequestParam Map<String, Object> param) {
        Query query = new Query(param);
        List<SysRoleRes> data = sysRoleResService.listData(query);
        int count = sysRoleResService.getCount(param);
        return renderPage(count, data);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.ADD_TITLE));
        return "sysRoleRes/add";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, Model model) {
        model.addAttribute("sysRoleRes", sysRoleResService.getById(id));
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl, pageTitle, Constant.EDIT_TITLE));
        return "sysRoleRes/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(SysRoleRes sysRoleRes) {
        String flId = sysRoleRes.getFlId();
        int result = 0;
        if (flId == null || "".equals(flId)) {
            result = sysRoleResService.save(sysRoleRes);
        } else {
            result = sysRoleResService.updateById(sysRoleRes);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public R batchDel(@RequestParam("ids") String[] ids) {
        int result = 0;
        result = sysRoleResService.batchDelByIds(ids);
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }
}
